package com.zy.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zy.common.pojo.EUDataGridResult;
import com.zy.common.pojo.TreeNode;
import com.zy.common.util.TaotaoResult;
import com.zy.pojo.TbItem;
import com.zy.pojo.TbItemCat;
import com.zy.pojo.TbItemDesc;
import com.zy.service.ItemCatService;
import com.zy.service.ItemService;

@Controller
@RequestMapping("/item")
public class ItemController {
	@Autowired
	private ItemService itemService;
	@Autowired
	private ItemCatService  itemCatService ;

	// 测试方法
	@ResponseBody
	@RequestMapping("/{itemId}")
	public TbItem getItemById(@PathVariable long itemId) { // 从路径中取值
		TbItem ibItem = itemService.getItemById(itemId);
		return ibItem;
	}

	// 商品列表
	@RequestMapping("/list")
	@ResponseBody
	public EUDataGridResult getItemList(int page, int rows) {
		EUDataGridResult result = itemService.getItemList(page, rows);
		return result;

	}

	@RequestMapping("/cat/list")
	@ResponseBody
	// 树的一级目录，默认父节点为0
	public List<TreeNode> getItemCatList(@RequestParam(value = "id", defaultValue = "0") Long id) {
		List<TreeNode> list = itemCatService.getItemCatList(id);	
		return list;
	}

	// 商品添加
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public TaotaoResult createItem(TbItem item, String desc) throws Exception {
		TaotaoResult result = itemService.addItem(item, desc);
		return result;
	}
	
	//商品编辑
	@ResponseBody
	@RequestMapping("/update")
	public TaotaoResult updateItem(TbItem tbItem) {
		return itemService.updateItem(tbItem);
	}

	//商品删除
	@ResponseBody
	@RequestMapping("/delete")
	public TaotaoResult deleteItem(String ids) {
		TaotaoResult result=new TaotaoResult();
		if(ids.contains(",")) {
			String[] split = ids.split(",");
			for (String s : split) {
				result = itemService.deleteItem(Long.valueOf(s));
			}
		}else {
			result =itemService.deleteItem(Long.valueOf(ids));
		}
		return result;
	}
}
